<template>
  <el-form
    :model="form"
    :rules="formRules"
    ref="formRef"
    label-width="auto"
    style="max-width: 400px"
  >
    <el-form-item label="手机号">
      <el-input v-model="form.mobile" disabled />
    </el-form-item>

    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="form.nickname" placeholder="请输入昵称" />
    </el-form-item>

    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="form.gender">
        <el-radio value="男">男</el-radio>
        <el-radio value="女">女</el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item label="生日" prop="birthday">
      <el-date-picker
        v-model="form.birthday"
        type="date"
        placeholder="请选择生日"
        style="width: 100%"
      />
    </el-form-item>

    <!-- 选择头像的表单项 -->
    <el-form-item label="头像">
      <el-upload
        class="avatar-uploader"
        action="#"
        :auto-upload="false"
        :show-file-list="false"
        :on-change="handleAvatarChange"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>

    <el-form-item label=" ">
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
import useUserStore from '../../store/userStore'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { putProfileApi } from '../../api'

const store = useUserStore()
const imageUrl = ref(store.user.photo || '')

// do not use same name with ref
const form = reactive({ ...store.user, photo: null })

const formRules = {
  nickname: [{ required: true, message: '昵称不能为空！' }],
  gender: [{ required: true, message: '性别不能为空！' }],
  birthday: [{ required: true, message: '生日不能为空！' }]
}

const formRef = ref(null)

const handleAvatarChange = (uploadFile) => {
  console.log('--- 选择了头像 ---')
  console.log(uploadFile)

  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  form.photo = uploadFile.raw
}

const onSubmit = () => {
  formRef.value.validate(async (vr) => {
    if (!vr) return

    // 封装并调用接口
    const fd = new FormData()
    fd.append('nickname', form.nickname)
    fd.append('gender', form.gender)
    fd.append('birthday', form.birthday)
    fd.append('photo', form.photo)
    await putProfileApi(fd)

    // 更新成功
    ElMessage.success('更新成功！')
    // 刷新用户的基本信息
    store.initUserProfile()
  })
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
